package com.controller.luo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.entity.Customer;
import com.entity.Drawtype;
import com.entity.Word;
import com.entity.Wordsel;
import com.github.pagehelper.PageInfo;
import com.service.luo.CusService;
import com.service.luo.DrawtypeService;
import com.service.luo.WordService;
import com.service.luo.WordselService;

@Controller
public class CustomerController {
	@Autowired
	private CusService cusService;
	@Autowired
	private WordService wordService;
	@Autowired
	private WordselService wordselService;
	@Autowired
	private DrawtypeService drawtypeService;
	
//	@RequestMapping("/getAllCus")
//	public List<Customer> getAllCus(){
//		return cusService.findAllCus();
//	}
	
	@GetMapping("/createCustomer")
	@ResponseBody
	public String createCustomer(String copname,String cname,String csource,String ctel,String cphone,String cremark){
		cusService.createOne(copname, cname, csource, ctel, cphone, cremark);
		return "SUCCESS";
	}
	
	@RequestMapping("/selectCuses")
	@ResponseBody
	public Map selectCuses(Customer cus,Integer start,Integer size)
	{
		return cusService.selectCus(cus,start,size);
	}
	
	@RequestMapping("/selectCusByCusno")
	@ResponseBody
	public Customer selectCusByCusno(Long cusno)
	{
		return cusService.selectCusByCusno(cusno);
	}
	
	@RequestMapping("/findAllWords")
	@ResponseBody
	public List<Word> findAllWords()
	{
		return wordService.findAllWord();
	}
	
	@GetMapping("/findWordselByWordno")
	@ResponseBody
	public List<Wordsel> findOneWordByNo(@RequestParam(name="wordno") Long wordno)
	{
		return wordselService.findWordselByWordno(wordno);
	}
	
	@PutMapping("/upateWordsels")
	@ResponseBody
	public String upateWordsels(@RequestBody /* @RequestParam(name="allWords") */ JSONArray allWords)
	{
		wordselService.updateWordsels((ArrayList<Wordsel>) JSONObject.parseArray(allWords.toString(),Wordsel.class));
		return "SUCCESS";
	}
	
	@GetMapping("/findWordsByWtno")
	@ResponseBody
	public List<Word> findWordsByWtno(Long wtno)
	{
		return wordService.findWordsByWtno(wtno);
	}
	
	@GetMapping("/findAllDtAsc")
	@ResponseBody
	public List<Drawtype> findAllDtAsc()
	{
		return drawtypeService.findAllDtAsc();
	}
	
	@PutMapping("/updateCus")
	@ResponseBody
	public void updateCus(@RequestBody Customer cus) {
		System.out.println(cus);
		cusService.updateCustomer(cus);
	}
	
	@DeleteMapping("/deleteCus")
	@ResponseBody
	public void deleteCus(Long cusno) {
		cusService.deleteCus(cusno);
	}
}
